package cn.com.dhcc.ddi.datasource.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.dao.SimpleDaoForOracle;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.dbhandler.SingleTransationCircleWithOutResult;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datasource.service.TableInfoService;
import cn.com.dhcc.ddi.datasource.vo.DataSourceVo;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
import cn.com.dhcc.ddi.datasource.vo.wapper.TableColRowWapper;
import cn.com.dhcc.ddi.datasource.vo.wapper.TableInfoRowWapper;
/**
 * 
 * DataSourceMysqlDao
 * @Author: fxx
 * @date: 2017-5-16
 *
 */
@Repository
public class DataSourceMysqlDao extends BaseDao {
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	private SimpleDaoForOracle dao;
	@Autowired
	public DataSourceDao datasourceDao;
	@Autowired
	TableInfoService tableInfoService;
	@Autowired
	TableInfoDao tableInfoDao;
	@Autowired
	TableColDao tableColDao;
	/**
	 * 现根据界面上的连接信息初始化数据源
	 * 
	 * @param vo
	 * @throws NiceException
	 * @throws ServiceException 
	 * @throws DaoException 
	 */
	public void initDao(DataSourceVo vo) throws DaoException {
		try {
			dao = datasourceDao.initDao(vo);
		} catch (DaoException e) {
			throw new DaoException("初始化数据源错误", e);
		}
	}

	
	/**
	 * 获取schema Mysql
	 * 
	 * @return
	 * @throws DaoException
	 */
	public String getSchemaForMysql() throws DaoException {
		String msg = "";
		String sql = "select DATABASE()";
		try {
			return dao.queryForString("获取schema", sql, new Object[] {});
		} catch (DaoException e) {
			msg = "获取数据库信息失败";
			throw new DaoException(msg, e);
		}

	}

	/**
	 * 
	 * @Title: getUserTablesForMysql
	 * @Description: 获取所有表信息
	 * @param @param schema
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return List<TableInfoVo> 返回类型
	 * @throws
	 */
	public List<TableInfoVo> getUserTablesForMysql(String schema)
			throws DaoException {
		String msg = "";
		String sql = " select t.table_name table_name,t.table_schema schema_name,t.table_comment from information_schema.tables t where t.table_schema = ? "
				    +" order by t.table_name ";
		try {
			return dao.list("获取所有被授权的表", sql, new TableInfoRowWapper(),
					new Object[] { schema });
		} catch (DaoException e) {
			msg = "获取所有表信息失败";
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 
	 * @Title: getUserTabColumnsForMysql
	 * @Description: 获取所有表字段信息
	 * @param @param tableName
	 * @param @param schema
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return List<TableColVo> 返回类型
	 * @throws
	 */
	public List<TableColVo> getUserTabColumnsForMysql(String tableName,
			String schema) throws DaoException {
		String msg = "";
		String sql ="select t.* from ("+
				    " select " +
					"	 t1.column_name COL_NAME, " +
					"	 t1.column_comment COL_COMMENT, " +
					"	 t1.data_type COL_TYPE, " +
					"	 t1.column_default COL_DEFAULT, " +
					"	 case " +
					"	 when t1.column_key in ('pri', 'uni') then " +
					"	 	'1' " +
					"	 else " +
					"	 	'0' " +
					"	 end is_pk"+					
					" from " +
					"	 information_schema. columns t1 " +		
					" where " +
					"	 t1.table_schema = ? and t1.table_name = ? "
					+") t order by t.is_pk desc, t.col_name";
		try {
			return dao.list("获取所有表字段信息", sql, new TableColRowWapper(),
					new Object[] { schema, tableName });
		} catch (DaoException e) {
			msg = "获取所有表字段信息失败";
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 
	 * @Title: insert
	 * @Description: 数据源信息入库
	 * @param @param vo
	 * @param @param tbInfoList
	 * @param @param tbColInfoList
	 * @param @throws DaoException 参数
	 * @return void 返回类型
	 * @throws
	 */
	public void insert(final DataSourceVo vo, final List<Object[]> tbInfoList, final List<Object[]> tbColInfoList) throws DaoException {
		try {
			CM.getDao().doInSingleTransationCircle("数据源信息入库", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						datasourceDao.insertDataSource(vo);
						tableInfoDao.insertTableInfo(tbInfoList);
						tableColDao.insertTableColInfo(tbColInfoList);
					} catch (DaoException e) {
						logger.error("数据源信息入库失败", e);
						throw new RuntimeException("数据源信息入库失败", e);
					}
				}
			});
		} catch (DaoException e) {
			throw new DaoException("数据源信息入库失败", e);
		}
	}
	

	

}
